/*
 * @Author: Jason Liu
 * @Date: 2024-12-26 13:47:09
 * @Desc: 
 */

const toolBar = require("./toolBar").default;

const { navigationList } = require("./config");

import hdwModuleGroup from "../hdwModuleGroup";

export default {
  components: { toolBar, hdwModuleGroup },
  props: {
    value: {
      type: Object,
      default: () => {
        return {
          id: undefined,
          code: undefined,
          name: undefined
        };
      }
    },
    close: {
      type: [Function, Boolean],
      default: false
    }
  },
  provide() {
    return {
      hdwList: () => {
        return this.hdwSources
      },
      updateModelHDWS: (val) => {
        this.hdwSources = val
      }
    }
  },
  data() {
    return {
      loading: false,
      viewSql: false,
      indicatorInfo: this.value,
      toolBarKey: 1,
      activeId: undefined,
      collapsed: false,
      stepCode: 0,
      deviseList: [],
      pageKey: 0,
      lock: false,
      hdwSources: []
    };
  },
  created() {
    // this.setIndicatorInfo({
    //   id: "350712405020164096",
    //   indicatorCode: "A01020010",
    //   indicatorDisplayName: "门诊超过预约时间做医技检查患者人数",
    //   indicatorName: "门诊超过预约时间做医技检查患者人数"
    // });
    this.init();
    window.addEventListener(
      "message",
      event => {
        const { code, data } = event.data;
        switch (code) {
          case "getOk":
            this.setIndicatorInfo(data);
            break;
          case "saveOk":
            this.setIndicatorInfo(data);
            //this.init();
            if (data.isSupplement == 1) { // isSupplement是否补录（0否 1是）
              this.stepCode = 4 // 补录跳转到指标配置阶段
            } else {
              this.stepCode++;
            }
            break;
          default:
            break;
        }
      },
      false
    );
  },
  methods: {
    /**
     * @Author: Jason Liu
     * @description: 设置指标信息
     */
    setIndicatorInfo(e) {
      let code = e.indicatorCode || e.indicatorId;
      if (this.indicatorInfo.code !== code && code) {
        this.indicatorInfo.id = e.id;
        this.indicatorInfo.code = code;
        this.indicatorInfo.name = e.indicatorName;
        this.init();
      }
    },
    init() {
      this.pageKey = moment().format("YYYYMMDDHH");
      let biCaliber_url = $storage.get("biCaliber_url");
      const { code } = this.indicatorInfo;

      this.deviseList = JSON.DeepCopy(navigationList).map((item, index) => {
        if (!this.activeId && index == 0) {
          this.activeId = item.code;
        }
        if (item.type == "biPage") {
          item.url = item.url.format({
            code: code || "",
            biCaliber_url: biCaliber_url,
            pageKey: this.pageKey
          });
        }
        return item;
      });
      this.structureDevise();
    },
    /**
     * @Author: Jason Liu
     * @description: 保存当前页面数据信息
     */
    async savePage() {
      let biCaliber_url = $storage.get("biCaliber_url");
      switch (this.activeId) {
        case "editIndicator": //指标维护
          this.$refs.editIndicator[0].contentWindow.postMessage(
            "indicatorSave",
            biCaliber_url
          );
          break;
        case "setIndicator": //指标配置
          this.$refs.setIndicator[0].contentWindow.postMessage(
            "indicatorSave",
            biCaliber_url
          );
          break;
        case "createHdw": //HDW爆粗
          this.$refs.createHdw[0].save(req => {
            this.stepCode++;
          });
          break;
        case "createDws":
          this.$refs.createDws[0].contentWindow.postMessage(
            "dwsSave",
            biCaliber_url
          );
          break;
        case "createAds":
          this.$refs.createAds[0].contentWindow.postMessage(
            "adsSave",
            biCaliber_url
          );
          break;
      }
    },

    async addPage() {
      let biCaliber_url = $storage.get("biCaliber_url");
      switch (this.activeId) {
        case "createHdw":
          this.$refs.createHdw[0].addNewModel();
          break;
        case "createDws":
          this.$refs.createDws[0].contentWindow.postMessage(
            "dwsAdd",
            biCaliber_url
          );
          break;
        case "createAds":
          this.$refs.createAds[0].contentWindow.postMessage(
            "adsAdd",
            biCaliber_url
          );
          break;
      }
    },
    /**
     * @Author: Jason Liu
     * @description: 同步保存
     */
    synchronous() {
      let biCaliber_url = $storage.get("biCaliber_url");
      switch (this.activeId) {
        case "createDws":
          this.$refs.createDws[0].contentWindow.postMessage(
            "dwsAsyncSave",
            biCaliber_url
          );
          break;
        case "createAds":
          this.$refs.createAds[0].contentWindow.postMessage(
            "adsAsyncSave",
            biCaliber_url
          );
          break;
      }
    },
    /**
     * 新增hdw模型
     */
    // addHdwModel() {
    //   this.$refs.createHdw[0].addNewModel();
    // },
    /**
     * 模型记录切换
     */
    changeModelRecord({ key }) {
      console.log("key", key);
      this.$refs.createHdw[0].queryModeolById({ id: key });
    },
    /**
     * @Author: Jason Liu
     * @description: 下面一个页面信息
     */

    nextPage() {},
    /**
     * @Author: Jason Liu
     * @description: 上一个页面信息
     */
    prevPage() {},
    /**
     * @Author: Jason Liu
     * @description: 刷洗页面
     */
    reloadPage() {},
    /**
     * @Author: Jason Liu
     * @description: 返回页面信息
     */

    goBack() {
      if (this.close) {
        this.close();
      } else {
        const referrerUrl = document.referrer;
        const origin = window.location.origin;
        if (referrerUrl.indexOf(origin) > -1) {
          $serviceJs.pageBack();
        } else {
          window.close();
        }
      }
    },
    structureDevise() {
      this.lock = true;
      this.loading = true;
      return new Promise(resolve => {
        let devise = this.deviseList[this.stepCode];
        this.activeId = devise.code;
        devise.structure = true;
        setTimeout(() => {
          resolve(true);
        }, 500);
      }).finally(() => {
        this.lock = false;
        this.loading = false;
      });
    }
  },
  watch: {
    stepCode(val) {
      this.structureDevise();
    },
    value(val) {
      this.indicatorInfo = val;
    }
  }
};
